package com.yj.consumer.config;

import feign.FeignException;
import feign.Response;
import feign.RetryableException;
import feign.codec.ErrorDecoder;

/**
 * 自定义feign解析处理器，在响应码为非2XX时触发
 */
public class MyErrorDecoder implements ErrorDecoder {
    @Override
    public Exception decode(String methodKey, Response response) {
        FeignException exception = FeignException.errorStatus(methodKey, response);
        if (response.status() == 502) {//feign接口响应状态为502，抛出可重试异常，触发重试机制
            return new RetryableException(response.status(),
                    exception.getMessage(),
                    response.request().httpMethod(),
                    exception,
                    null,
                    response.request());
        } else {
            return exception;
        }
    }
}
